<?php

class User_Controller_Admin_Manage extends Controller
{

    public function actionIndex($request, $response)
    {
        $table = Agis::getTable('user@user');

        $tableRoleName = Agis::getTableName('core_roles');

        $tableUserName = Agis::getTableName('users');

        $select = $table -> select() -> from("$tableUserName", "$tableUserName.*") -> setIntegrityCheck(false) -> joinLeft($tableRoleName, "$tableRoleName.role_id=$tableUserName.role_id", array("$tableRoleName.role"));

        $response -> paginator = $paginator = Zend_Paginator::factory($select);

        $paginator -> setCurrentPageNumber($request -> getParam('page', 1));

    }

    public function actionAdd($request, $response)
    {
        $form = $response -> form = Agis::getTable('core@form') -> getForm('admin.user.create');

        if ($request -> isPost() && $form -> isValid($_POST))
        {
            $data = $form -> getValues();

            $table = Agis::getTable('user@user');

            $user = $table -> fetchNew();

            $user -> setFromArray($data);

            $user -> creation_date = $user -> modification_date = date('Y-m-d H:i:s');
            $user -> setPassword($data['password']);

            $user -> save();
            $this -> redirect('admin/user/manage');
        }

        $response -> setScript('module/core/views/admin/edit.tpl');
    }

}
